﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils.Common;

namespace ZK.MyControl
{
   public  class DataGridColumnHelper
    {



        public static DataGridViewTextBoxColumn TextBoxColumnCreate(Type type, string propertyName ,bool isreadOnly =false )
        {
            DataGridViewTextBoxColumn tbc = new DataGridViewTextBoxColumn();
            tbc.Name = propertyName;
            tbc.HeaderText = PropertyHelper.GetDescription(type, propertyName);
            tbc.DataPropertyName = propertyName;
            tbc.ReadOnly = isreadOnly;
  
            return tbc;

        }

        public static DataGridViewTextBoxColumn TextBoxColumnCreate(Type type, string propertyName, string format,bool isreadOnly = false)
        {
            DataGridViewTextBoxColumn tbc = new DataGridViewTextBoxColumn();
            tbc.Name = propertyName;
            tbc.HeaderText = PropertyHelper.GetDescription(type, propertyName);
            tbc.DataPropertyName = propertyName;
            tbc.ReadOnly = isreadOnly;
            tbc.DefaultCellStyle.Format = format;
            return tbc;

        }

        public static DataGridViewTextBoxColumn TextBoxColumnCreate(  string propertyName,string headertext, bool isreadOnly = false )
        {
            DataGridViewTextBoxColumn tbc = new DataGridViewTextBoxColumn();
            tbc.Name = propertyName;
            tbc.HeaderText = headertext;
            tbc.DataPropertyName = propertyName;
            tbc.ReadOnly = isreadOnly;
            
            return tbc;

        }

        public static DataGridViewCheckBoxColumn CheckBoxColumnCreate(Type type, string propertyName, bool isreadOnly = false )
        {
            DataGridViewCheckBoxColumn cbc = new DataGridViewCheckBoxColumn();
            cbc.Name = propertyName;
            cbc.HeaderText = PropertyHelper.GetDescription(type, propertyName);
            cbc.DataPropertyName = propertyName;
            cbc.ReadOnly = isreadOnly;
            
            return cbc;

        }

        public static DataGridViewComboBoxColumn ComboBoxColumnCreate(Type type, string propertyName,object objSource ,string displayMember=null,string valueMember = null,   bool isreadOnly = false)
        {
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.Name = propertyName;
            cbc.HeaderText = PropertyHelper.GetDescription(type, propertyName);
            cbc.DataPropertyName = propertyName;
            cbc.DataSource = objSource;
            if (!string.IsNullOrEmpty(displayMember))
                cbc.DisplayMember = displayMember;
            if (!string.IsNullOrEmpty(valueMember))
                cbc.ValueMember = valueMember;
            cbc.ReadOnly = isreadOnly;
            return cbc;

        }


        public static DataGridViewButtonColumn ButtonColumnCreate(string name ,string headerText, string text,string dataPropertyName=null)
        {
            DataGridViewButtonColumn btnEdit = new DataGridViewButtonColumn();
            btnEdit.Name = name;
            btnEdit.HeaderText = headerText;
            btnEdit.FlatStyle = FlatStyle.Popup;
            if (!string.IsNullOrEmpty(dataPropertyName))
            {
                btnEdit.DataPropertyName = dataPropertyName;
            }
       
            btnEdit.Text = text;
            btnEdit.UseColumnTextForButtonValue = true;
        
            return btnEdit;

        }

 
        public static DataGridViewLinkColumn LinkColumnCreate(string headertext, string propertyName, string text, bool isreadOnly = true)
        {
            DataGridViewLinkColumn dataGridViewLinkColumn = new DataGridViewLinkColumn();
            dataGridViewLinkColumn.Name = propertyName;
            dataGridViewLinkColumn.HeaderText = headertext;
            dataGridViewLinkColumn.ReadOnly = isreadOnly;
            dataGridViewLinkColumn.UseColumnTextForLinkValue = true;
            dataGridViewLinkColumn.Text = text;
 
            return dataGridViewLinkColumn;
        }
    }
}
